import {clickMove} from "utils/experiment/ex2/AnimePlay";

// 正则判断是否为整数
function is_num(num) {
  if (num === "") {
    return false;
  }
  return /(^[1-9]\d*$)/.test(num);
}

// 是否为数字
function checkNum(num) {
  if (num === "") {
    return false;
  }
  return !isNaN(num);
}

function check(operations) {
  let wrong_line = [];
  for (let i = 0; i < operations.length; ++i) {
    if (operations[i] === undefined || operations[i].length === 0) continue
    let operation = operations[i].split(' ');
    let index = i + 1;
    index = index.toString();
    if (operation[0] === "#plane") {
      if (operation.length !== 2) {
        wrong_line.push(index);
      } else if (!is_num(operation[1])) {
        wrong_line.push(index);
      }
    } else if (operation[0] === "setSpeed") {
      if (operation.length === 1) {
        wrong_line.push(index);
      } else if (!checkNum(operation[1])) {
        wrong_line.push(index);
      } else if (operation[1] < 0) {
        wrong_line.push(index);
      }

    } else if (operation[0] === "move") {
      if (operation.length === 1) {
        wrong_line.push(index);
      } else if (!checkNum(operation[1])) {
        wrong_line.push(index);
      } else if (operation[1] < 0) {
        wrong_line.push(index);
      }

    } else if (operation[0] === "setAngle") {
      if (operation.length === 1) {
        wrong_line.push(index);
      } else if ((operation.length - 1) % 4 !== 0) {
        wrong_line.push(index);
      } else if (operation[1] > 1 || operation[2] > 1 || operation[3] > 1 || operation[4] > 1 || operation[1] < -1 || operation[2] < -1 || operation[3] < -1 || operation[4] < -1) {
        wrong_line.push(index);
      } else if (!checkNum(operation[1]) || !checkNum(operation[2]) || !checkNum(operation[3]) || !checkNum(operation[4])) {
        wrong_line.push(index);
      }

    } else {
      wrong_line.push(index);
    }
  }
  return wrong_line;
}

function operate(operations) {
  let operation_list = []
  for (let i = 0; i < operations.length; ++i) {
    let operation = operations[i].split(' ');
    let temp_list = []
    if (operation[0] === "#plane") {
      temp_list.push('plane')
      temp_list.push(operation[1])
    } else if (operation[0] === "setSpeed") {
      temp_list.push(operation[0])
      temp_list.push(operation[1])
    } else if (operation[0] === "setAngle") {
      temp_list.push(operation[0])
      temp_list.push(operation[1])
      temp_list.push(operation[2])
      temp_list.push(operation[3])
      temp_list.push(operation[4])
    } else if (operation[0] === "move") {
      temp_list.push(operation[0])
      temp_list.push(operation[1])
    }
    operation_list.push(temp_list)
  }
  return operation_list
}

// 翻译代码
function translate(operations) {
  operations = operations.split('\n')
  let wrong_line = check(operations);
  if (wrong_line.length !== 0) {
    let message = "第";
    for (let i = 0; i < wrong_line.length; ++i) {
      if (i !== 0) {
        message += ", ";
      }
      message += wrong_line[i];
    }
    message += "行代码出错"
    return {
      'status': 501,
      'msg': message
    }
  }
  let operation_list = operate(operations)
  return {
    'status': 200,
    'msg': '代码编译成功',
    'data': operation_list
  }
}

export function checkAndRun(_this) {
  let res = translate(_this.code)
  if (res.status === 501) {
    let message = res.msg + "!请再检查一下哦！";
    _this.$alert(message, '提示', {
      confirmButtonText: '确定'
    })
  } else {
    let data = res.data
    let plane_id = 0;
    let time = [], speed = [], direction = []
    for (let i = 0; i < 13; ++i) {
      time[i] = [];
      speed[i] = [];
      direction[i] = []
    }
    for (let i = 0; i < data.length; ++i) {
      if (data[i][0] === 'plane') {
        plane_id = data[i][1];
      } else if (data[i][0] === 'move') {
        time[plane_id - 1].push(parseInt(data[i][1]));
      } else if (data[i][0] === 'setSpeed') {
        speed[plane_id - 1].push(parseInt(data[i][1]));
      } else if (data[i][0] === 'setAngle') {
        direction[plane_id - 1].push(data[i][1]);
        direction[plane_id - 1].push(data[i][2]);
        direction[plane_id - 1].push(data[i][3]);
        direction[plane_id - 1].push(data[i][4]);
      }
    }
    _this.$message({
      message: '代码编译成功，开始运行！',
      type: 'success',
      duration: 1000,
      offset: 50
    });
    _this.time = time;
    _this.speed = speed;
    _this.direction = direction
    _this.isFly = true;
    clickMove(_this)
  }
}

export function checkZ(planeArr) {
  planeArr.sort(function (a, b) {
    if (a.posy !== b.posy) return a.posy < b.posy;
    if (a.posx !== b.posx) return a.posx < b.posx;
    else return a.posz < b.posz;
  })
  for (let now = 0; now < 4; ++now) {
    let next = now + 1;
    let gapOfY = Math.abs(planeArr[now].posy - planeArr[next].posy);
    let gapOfZ = Math.abs(planeArr[now].posz - planeArr[next].posz);
    if (gapOfY > 100 || gapOfZ > 100) {
      return false;
    }
  }
  for (let now = 8; now < 12; ++now) {
    let next = now + 1;
    let gapOfY = Math.abs(planeArr[now].posy - planeArr[next].posy);
    let gapOfZ = Math.abs(planeArr[now].posz - planeArr[next].posz);
    if (gapOfY > 100 || gapOfZ > 100) {
      return false;
    }
  }
  return true;
}
